//
//  NewsViewController.m
//  KuWoCan
//
//  Created by hwd on 17/3/2.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsCell.h"
#import "NewsDetailViewController.h"
#import "NWNewsRequest.h"
#import "NewsListModel.h"


@interface NewsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableV;

@property (nonatomic,assign) NSInteger currentPage;

@property (nonatomic,strong) NSMutableArray *tableArray;

@end

@implementation NewsViewController
#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    [self configRefreshControl];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    AppDelegate *applicationDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [applicationDelegate.tabBarController hideTabBar:NO];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    AppDelegate *applicationDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [applicationDelegate.tabBarController hideTabBar:YES];
}

#pragma mark - Config UI
- (void)configUI {
    self.title = @"新闻资讯";
    [self createRightButtonWithImage:[UIImage imageNamed:@"customer"] selector:@selector(CallPhone)];
    self.view.backgroundColor = KwBgColor;
    [self.view addSubview:self.tableV];
}
-(void)CallPhone//拨打客服
{
    [Constants callTelephoneWithPhone:@"4000716186" atViewController:self];
}
- (void)configRefreshControl{
    __weak typeof(self) weakSelf = self;
    self.tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.currentPage = 1;
        [weakSelf.tableV.mj_footer resetNoMoreData];
        [weakSelf loadingDataWithTag:1 showLoading:YES];
    }];
    [self.tableV.mj_header beginRefreshing];

    self.tableV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadingDataWithTag:0 showLoading:YES];
    }];
}

#pragma mark - Load data


#pragma mark - Data request

- (void)loadingDataWithTag:(NSInteger)tag showLoading:(BOOL)isShowLoading
{
    __weak typeof(self) weakSelf = self;
    NWNewsRequest *request = [[NWNewsRequest alloc] init];
    [request startRequestGetNewsListWithDelFlag:@"0" pageNo:[NSString stringWithFormat:@"%ld",(long)_currentPage] pageSize:kPageSize];
    [request completion:^(id result, NSString *message, id extension, BOOL succ) {
        [weakSelf stopRefresh];
        if (succ) {
            if (result && result != nil)
            {
                if (tag == 1) {
                    [self.tableArray removeAllObjects];
                }
                NSArray *arr = [NewsListModel arrayOfModelsFromDictionaries:result];
                [weakSelf.tableArray addObjectsFromArray:arr];
                if ([arr count] == [kPageSize integerValue])
                {
                    _currentPage ++ ;//有下一页  show 加载按钮
                }else
                {
                    //没有下一页  hide 加载按钮
                    [weakSelf.tableV.mj_footer endRefreshingWithNoMoreData];
                }
                    [_tableV reloadData];
            }
        } else {
            [SVProgressHUD showErrorWithStatus:message];
        }
    }];
}

#pragma mark - Registration notice and release


#pragma mark - UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 116 * screenW_6;

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.000001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000001;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsCell"];
    if (cell == nil) {
        cell  = [[NewsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NewsCell"];
    }

    cell.model = self.tableArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsDetailViewController *detailVC = [[NewsDetailViewController alloc]init];
    NewsListModel *model = self.tableArray[indexPath.row];
    detailVC.newsId = model.id;
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - event response


#pragma mark - private method
- (void)stopRefresh {
    if ([self.tableV.mj_header isRefreshing]) {
        [self.tableV.mj_header endRefreshing];
    }
    if ([self.tableV.mj_footer isRefreshing]) {
        [self.tableV.mj_footer endRefreshing];
    }
}

#pragma mark - getter and setter

- (UITableView *)tableV {
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 60) style:UITableViewStylePlain];
        _tableV.delegate = self;
        _tableV.dataSource = self;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.showsHorizontalScrollIndicator = NO;
    }
    return _tableV;
}

- (NSMutableArray *)tableArray {
    if (!_tableArray) {
        _tableArray = [NSMutableArray array];
    }
    return _tableArray;
}

@end
